#include "UtilityString.h"
#define  SMALLSTRLENS 255

UtilityString::UtilityString(void):m_iStrLens(SMALLSTRLENS), m_wstrContent(NULL)
{
	m_strContent = new char[m_iStrLens+1];
	memset(m_strContent,0,sizeof(char)*(m_iStrLens+1));
}

UtilityString::UtilityString(char* str1):m_wstrContent(NULL)
{
	if(str1!=NULL)
	{
		int iLens = strlen(str1);
		m_strContent = new char[iLens+1];
		memset(m_strContent,0,sizeof(char)*(iLens+1));
		strcpy(m_strContent,str1);
		m_iStrLens = strlen(m_strContent);
	}
}

UtilityString::UtilityString(wchar_t* wStr1):m_wstrContent(NULL)
{
	if(wStr1!=NULL)
	{
		int iLens = wcslen(wStr1);
		m_strContent = new char[iLens+1];
		memset(m_strContent,0,sizeof(char)*(iLens+1));
		wcstombs(m_strContent, wStr1, iLens+1);		
		m_iStrLens = strlen(m_strContent);
	}
}

UtilityString::UtilityString(UtilityString& uStr1):m_wstrContent(NULL)
{
	int iLens = uStr1.GetStrLens();
	m_strContent = new char[iLens+1];
	memset(m_strContent,0,sizeof(char)*(iLens+1));
	strcpy(m_strContent,uStr1.GetCharStr());
	m_iStrLens = strlen(m_strContent);
}

UtilityString::~UtilityString(void)
{
	if(m_strContent!=NULL)
		delete[] m_strContent;

	if(m_wstrContent!=NULL)
		delete[] m_wstrContent;
}


void UtilityString::SetCharStr(char* str1)
{
	if(!str1)
		return;
	int iLens = strlen(str1);
	if(m_strContent)
		delete[] m_strContent;
	m_strContent = new char[iLens+1];
	memset(m_strContent,0,sizeof(char)*(iLens+1));
	strcpy(m_strContent, str1);
	m_iStrLens = strlen(m_strContent);
}

void UtilityString::SetWCharStr(wchar_t* wStr1)
{
	if(!wStr1)
		return;

	int iLens = wcslen(wStr1);	
	if(m_strContent)
		delete[] m_strContent;
	m_strContent = new char[iLens+1];
	memset(m_strContent,0,sizeof(char)*(iLens+1));
	wcstombs(m_strContent,wStr1,iLens+1);
	m_iStrLens = strlen(m_strContent);	
}

wchar_t* UtilityString::GetWCharStr()
{
	if(!m_strContent)
		return NULL;

	if(m_wstrContent)
		delete[] m_wstrContent;

	m_wstrContent = new wchar_t[m_iStrLens+1];

	memset(m_wstrContent,0,sizeof(wchar_t)*(m_iStrLens+1));

	mbstowcs(m_wstrContent,m_strContent,m_iStrLens+1);

	return m_wstrContent;
}

void UtilityString::RelocateMemory(int iNewSize)
{
	if(iNewSize <= m_iStrLens)
		return;

	char* tmp = new char[m_iStrLens+1];
	memset(tmp,0,sizeof(char)*(m_iStrLens+1));
	strcpy(tmp, m_strContent);

	delete[] m_strContent;
	m_strContent = new char[iNewSize+1];
	memset(m_strContent,0,sizeof(char)*(iNewSize+1));
	strcpy(m_strContent,tmp);

	delete[] tmp;
}

void UtilityString::replaceExt(char* strExt)
{
	if(!strExt || !m_strContent)
		return;

	char* pPos = strrchr(m_strContent, '.');
	int iLens = strlen(strExt);
	int iNewSize = m_iStrLens - strlen(pPos+1) +  iLens;
	RelocateMemory(iNewSize);

	pPos = strrchr(m_strContent, '.');
	*(pPos+1) = NULL;
	strcat(m_strContent,strExt);
	m_iStrLens = strlen(m_strContent);

}

void UtilityString::ConcatenateStr(char* str1)
{
	if(!str1 || !m_strContent)
		return;

	int iLens = strlen(str1);
	int iNewSize = m_iStrLens + iLens;
	RelocateMemory(iNewSize);

	strcat(m_strContent,str1);
	m_iStrLens = strlen(m_strContent);
}

void UtilityString::ConcatenateWStr(wchar_t* wStr1)
{
	if(!wStr1 || !m_strContent)
		return;

	char* tmp = new char[wcslen(wStr1)+1];
	wcstombs(tmp,wStr1,wcslen(wStr1)+1);

	int iLens = strlen(tmp);
	int iNewSize = m_iStrLens + iLens;
	RelocateMemory(iNewSize);

	strcat(m_strContent,tmp);
	m_iStrLens = strlen(m_strContent);

	delete[] tmp;
}

void UtilityString::ConcatenateUtilityStr(UtilityString& uStr1)
{
	if(!m_strContent)
		return;

	ConcatenateStr(uStr1.GetCharStr());
}

int UtilityString::Remove1LevelSubDirectory()
{
	if (!m_strContent)
		return -1;

	char* pPos = strrchr(m_strContent,'\\');
	if(pPos != NULL){
		*pPos = '\0';
		m_iStrLens=strlen(m_strContent);
		return 0;
	}
	else
		return -1;
}

int UtilityString::SearchSubStringFirstOccurrence(char* str1)
{
	if(!m_strContent)
		return -1;

	int i,j,firstOcc;
	i=0,j=0;

	while(m_strContent[i]!='\0')
	{
		while(m_strContent[i]!=str1[0] && m_strContent[i]!='\0')
			i++;
		if(m_strContent[i]=='\0')
			return -1;     //search can not continue

		firstOcc = i;

		while(m_strContent[i]==str1[j] && m_strContent[i]!='\0' && str1[j]!='\0')
		{
			i++;
			j++;
		}

		if(str1[j]=='\0')
			return (firstOcc);
		if(m_strContent[i]=='\0')
			return (-1);

		i = firstOcc + 1;
		j = 0;
	}
	return -1;
}

int UtilityString::GenerateFormatSmallString(const char* format, ...)
{
	if(m_strContent)
		delete[] m_strContent;
	m_strContent = new char[SMALLSTRLENS+1];
	memset(m_strContent,0,sizeof(char)*SMALLSTRLENS+1);

	va_list args;
	va_start(args, format);
	vsprintf(m_strContent,format, args);// This still uses standard formating
	va_end(args);

	m_iStrLens = strlen(m_strContent);
	return 0;// Before return you can redefine it back if you want...
}